/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       interface_help.cpp

	$Header: /game/interface_help.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "interface_help.h"

#include "game_window.h"
#include "item_help_block.h"
#include "table.h"
#include "table_ptr.h"

namespace
{
	typedef t_counted_ptr<t_help_block> t_help_block_ptr;

	class t_help_table 
	{
	public:
		t_help_table();

		t_help_block const& operator[]( std::string const& window_name ) const;
	protected:
		typedef std::map< std::string, t_help_block_ptr, t_string_insensitive_less > t_map;

		t_map m_map;
	};

	inline t_help_block const& t_help_table::operator[]( std::string const& window_name ) const
	{
		t_map::const_iterator item = m_map.find( window_name );

		return *item->second;
	}

	static t_item_help const k_empty_help( "", "" );

}; // unnamed namespace

std::string t_help_block::get_name( std::string const& keyword ) const
{
	t_map::const_iterator item = m_map.find( keyword );

	if (item == m_map.end())
		return "";
	return item->second->name;
}

std::string t_help_block::get_help( std::string const& keyword ) const
{
	t_map::const_iterator item = m_map.find( keyword );

	if (item == m_map.end())
		return "";
	return item->second->help_text;
}

t_item_help const& t_help_block::operator[] (std::string const& keyword ) const
{
	t_map::const_iterator item = m_map.find( keyword );

	if (item == m_map.end())
		return k_empty_help;
	return *item->second;
}

enum
{
	k_column_window_name,
	k_column_keyword,
	k_column_name,
	k_column_help_text
};

t_help_table::t_help_table()
{
	t_table_cache		table_cache( "interface" );
	t_table_ptr			table = table_cache.get();
	t_table::iterator	line = table->begin();
	std::string         window_name;
	std::string         name;
	std::string         help;

	line++;
	for (; line != table->end(); line++)
	{
		if ((*line)[0].empty())
			continue;

		window_name = (*line)[k_column_window_name];

		t_help_block_ptr item;
		t_map::iterator  index = m_map.find( window_name );

		if (index != m_map.end())
			item = index->second;
		else
		{
			item = new t_help_block;
			m_map.insert( t_map::value_type( window_name, item ) );
		}
		if (line->size() > k_column_name)
			name = (*line)[k_column_name];
		else
			name = "";
		if (line->size() > k_column_help_text)
			help = (*line)[k_column_help_text];
		else
			help = std::string();
		item->add( (*line)[k_column_keyword], name,help );
	}
}

t_help_block const& get_help_block( std::string const& window_name )
{
	static t_help_table const table;

	return table[window_name];
}

std::string get_element_name( std::string const& window_name, 
							  std::string const& keyword )
{
	return get_help_block( window_name ).get_name( keyword );
}

std::string get_element_help( std::string const& window_name,
							  std::string const& keyword )
{
	return get_help_block( window_name ).get_help( keyword );
}


void set_help( t_window* window, t_help_block const& help, std::string const& keyword )
{
	t_item_help const& item = help[keyword];

	window->set_help_balloon_text( item.name );
	window->set_right_click_text( item.help_text );
}

// Only set balloon.
void set_help_balloon( t_window* window, t_help_block const& help, std::string const& keyword )
{
	t_item_help const& item = help[keyword];

	window->set_help_balloon_text( item.name );
}
